﻿package  {
	
	import flash.display.MovieClip;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class Stage extends MovieClip {
		
		var background:Background;		
		var chara:Chara;
		var defaultX:int;
		var gameTimer:Timer;
		var formatText:TextFormat;		
		var text1:TextField;		
		var text2:TextField;		
		var text3:TextField;		
		var text4:TextField;
		//var sound1:soundtest;
		var soundchannel:SoundChannel;
		
		var nama:String;
		
		// Component
		var bubble1:Bubble1;
		var bubble2:Bubble2;
		var bubble3:Bubble3;
		
		// Menu
		var ayoBelajar:AyoBelajar;
		var mariBermain:MariBermain;
		var latihanYuk:LatihanYuk;
		
		var bangunDatar:BangunDatar;
		var bilangan:Bilangan;
		var operasi:Operasi;
		
		var menuUtama:MenuUtama;
		var pilihMateri:PilihMateri;
		
		var state:int;
		var waktu:int;
		//var tes : 
		
		var flow:int;
		var changeFinished:Boolean;
		
		// is state?
		var isStateMenu:Boolean;
		
		// TAMBAHAN PUNYA ZAKIY
		var sound:Soundused;
				//selamat datang
		var sound1:s1;
		//belajar angka 1 permen
		var sound2:s2;
		//belajar angka 1
		var sound3:s3;
		//belajar angka 2
		var sound4:s4;
		//belajar angka 3
		var sound5:s5;
		//belajar bangun datar lingkaran
		var sound6:s6;
		//belajar bangun datar pembukaan
		var sound7:s7;
		//belajar bangun datar segiempat
		var sound8:s8;
		//belajar bangun datar segienam
		var sound9:s9;
		//belajar bangun datar segilima
		var sound10:s10;
		//belajar bangun datar segitiga
		var sound11:s11;
		//belajar bilangan pembukaan
		var sound12:s12;
		//belajar operasi pembukaan
		var sound13:s13;
		//belajar pengukuran lama waktu		
		var sound14:s14;
		//belajar pengukuran menentukan waktu
		var sound15:s15;
		//belajar pengukuran pembukaan
		var sound16:s16;
		//latihan bilangan angka berapa
		var sound17:s17;
		//latihan bilangan pembukaan
		var sound18:s18;
		//latihan jawaban delapan
		var sound19:s19;
		//latihan jawaban delapan belas
		var sound20:s20;
		//latihan jawaban dua
		var sound21:s21;
		//latihan jawaban dua belas
		var sound22:s22;
		//latihan jawaban dua puluh
		var sound23:s23;
		//latihan jawaban empat
		var sound24:s24;
		//latihan jawaban empat belas
		var sound25:s25;
		//latihan jawaban enam
		var sound26:s26;
		//latihan jawaban enam belas
		var sound27:s27;
		//latihan jawaban kamu benar
		var sound28:s28;
		//latihan jawaban kamu salah
		var sound29:s29;
		//latihan jawaban lima
		var sound30:s30;
		//latihan jawaban lima belas
		var sound31:s31;
		//latihan jawaban satu
		var sound32:s32;
		//latihan jawaban sebelas
		var sound33:s33;
		//latihan jawaban sembilan
		var sound34:s34;
		//latihan jawaban sembilan belas
		var sound35:s35;
		//latihan jawaban sepuluh
		var sound36:s36;
		//latihan jawaban tiga
		var sound37:s37;
		//latihan jawaban tiga belas
		var sound38:s38;
		//latihan jawaban tujuh
		var sound39:s39;
		//latihan jawaban tujuh belas
		var sound40:s40;
		//latihan operasi pembukaan
		var sound41:s41;
		//latihan pengukuran banding panjang
		var sound42:s42;
		//latihan pengukuran durasi
		var sound43:s43;
		//latihan pengukuran jam
		var sound44:s44;
		//latihan pengukuran pembukaan
		var sound45:s45;
		//ujian bangun datar pembukaan
		var sound46:s46;
		//ujian bilangan pembukaan
		var sound47:s47;
		//ujian kembali ke menu utama
		var sound48:s48;
		//ujian nilai jelek
		var sound49:s49;
		//ujian nilai sudah bagus
		var sound50:s50;
		//ujian operasi pembukaan
		var sound51:s51;
		//ujian pembukaan
		var sound52:s52;
		//ujian pengukuran pembukaan
		var sound53:s53;
		//ujian petunjuk pengerjaan
		var sound54:s54;
		//ujian waktu habis
		var sound55:s55;
		
		
		public function InitSoundused() {
			// constructor code
			sound1 = new s1();
			sound2 = new s2();
			sound3 = new s3();
			sound4 = new s4();
			sound5 = new s5();
			sound6 = new s6();
			sound7 = new s7();
			sound8 = new s8();
			sound9 = new s9();
			sound10 = new s10();
			sound11 = new s11();
			sound12 = new s12();
			sound13 = new s13();
			sound14 = new s14();
			sound15 = new s15();
			sound16 = new s16();
			sound17 = new s17();
			sound18 = new s18();
			sound19 = new s19();
			sound20 = new s20();
		}
		
				
		public function Stage() {
			// constructor codes
			init();
			initText();
			InitSoundused();
			
			
			sound = new Soundused();
			soundchannel = new SoundChannel();
			soundchannel = sound1.play();
			
			// Add Event Listener	
			//  =====================================================
			//  Main Menu
			//  =====================================================	
			ayoBelajar.addEventListener(MouseEvent.CLICK, Menu1);
			mariBermain.addEventListener(MouseEvent.CLICK, Menu2);
			latihanYuk.addEventListener(MouseEvent.CLICK, Menu3);
			
			ayoBelajar.addEventListener(MouseEvent.MOUSE_OVER, mouseOver1);
			ayoBelajar.addEventListener(MouseEvent.MOUSE_OUT, mouseOut1);
			mariBermain.addEventListener(MouseEvent.MOUSE_OVER, mouseOver2);
			mariBermain.addEventListener(MouseEvent.MOUSE_OUT, mouseOut2);
			latihanYuk.addEventListener(MouseEvent.MOUSE_OVER, mouseOver3);
			latihanYuk.addEventListener(MouseEvent.MOUSE_OUT, mouseOut3);	
			
			//  =====================================================
			//  Menu Belajar
			//  =====================================================
			
			bangunDatar.addEventListener(MouseEvent.CLICK, Menu11);
			bilangan.addEventListener(MouseEvent.CLICK, Menu12);
			operasi.addEventListener(MouseEvent.CLICK, Menu13);
			
			bangunDatar.addEventListener(MouseEvent.MOUSE_OVER, mouseOver11);
			bangunDatar.addEventListener(MouseEvent.MOUSE_OUT, mouseOut11);
			bilangan.addEventListener(MouseEvent.MOUSE_OVER, mouseOver12);
			bilangan.addEventListener(MouseEvent.MOUSE_OUT, mouseOut12);
			operasi.addEventListener(MouseEvent.MOUSE_OVER, mouseOver13);
			operasi.addEventListener(MouseEvent.MOUSE_OUT, mouseOut13);	
			
			// construction timer
			gameTimer = new Timer(80);
			gameTimer.addEventListener(TimerEvent.TIMER, stateGame);			
			gameTimer.start();
		}
		
		public function init() {
			// inisiasi
			state = 0;
			waktu = 0;
			flow = 0;
			
			// isState
			isStateMenu = true;
			changeFinished = false;
			
			// screen
			defaultX = 30;
			this.stage.displayState = StageDisplayState.FULL_SCREEN;
			
			// contructor Background
			background = new Background();
			addChild(background);
			background.x = (this.stage.width/2)-(background.width/2) + defaultX;
			
			// constructor chara
			chara = new Chara();
			addChild(chara);
			chara.x = defaultX + 800;
			chara.y = 500;
			
			// constructor buuble1
			bubble1 = new Bubble1();
			addChild(bubble1);
			bubble1.x = defaultX + 297.8;
			bubble1.y = 176.35;
			bubble1.visible = false;
			
			// constructor buuble2
			bubble2 = new Bubble2();
			addChild(bubble2);
			bubble2.x = defaultX + 600.65;
			bubble2.y = 8.75;
			
			// Main Menu
			// constructor menu utama
			menuUtama = new MenuUtama();
			addChild(menuUtama);
			menuUtama.x = defaultX + 30;
			menuUtama.y = 0;
			menuUtama.alpha = 0.7;
			menuUtama.visible = false;
			
			// constructor pilih materi
			pilihMateri = new PilihMateri();
			addChild(pilihMateri);
			pilihMateri.x = defaultX + 158.3;
			pilihMateri.y = -2;
			pilihMateri.alpha = 0.7;
			pilihMateri.visible = false;
			
			// constructor ayo belajar
			ayoBelajar = new AyoBelajar();
			addChild(ayoBelajar);
			ayoBelajar.x = defaultX + 92.75;
			ayoBelajar.y = 73.25;
			ayoBelajar.alpha = 0.7;
			
			// constructor mari Bermain
			mariBermain = new MariBermain();
			addChild(mariBermain);
			mariBermain.x = defaultX + 28.25;
			mariBermain.y = 312.85;
			mariBermain.alpha = 0.7;
			
			// constructor latihan yuk
			latihanYuk = new LatihanYuk();
			addChild(latihanYuk);
			latihanYuk.x = defaultX + 189.2;
			latihanYuk.y = 498.85;
			latihanYuk.alpha = 0.7;
			
			// constructor Bangun Datar
			bangunDatar = new BangunDatar();
			addChild(bangunDatar);
			bangunDatar.x = defaultX + 89.1
			bangunDatar.y = 119.9;
			bangunDatar.alpha = 0;
			bangunDatar.visible = false;
			
			// constructor Bilangan
			bilangan = new Bilangan();
			addChild(bilangan);
			bilangan.x = defaultX + 89.1;
			bilangan.y = 298.85;
			bilangan.alpha = 0;
			bilangan.visible = false;
			
			// constructor Operasi
			operasi = new Operasi();
			addChild(operasi);
			operasi.x = defaultX + 89.1;
			operasi.y = 483.5;
			operasi.alpha = 0;
			operasi.visible= false;
		}
		
		public function initText() {
			// constructor Text
			text1 = new TextField();
			text2 = new TextField();
			text2.width = 600;	
			text3 = new TextField();
			text3.width = 600;	
			text4 = new TextField();
			text4.width = 600;	
			
			text1.x = bubble2.x + 60;
			text1.y = bubble2.y + 80;
			text1.width = 600;	
			
			formatText = new TextFormat();			
			setFormatText(1,"Hai teman-teman", 40, "First Grader", "0x947000")
			addChild(text1);
			addChild(text2);
			addChild(text3);
			addChild(text4);
		}
		
		public function setFormatText(numberKal:int, kalimat:String, size:int, font:String, color:String):void {			
			formatText.size = size;
			formatText.font = font;
			formatText.color = color;
			
			if (numberKal == 1)	{
				text1.text = kalimat;				
				text1.setTextFormat(formatText);
			}
			
			else if (numberKal == 2)	{
				text2.text = kalimat;				
				text2.setTextFormat(formatText);
			}
			
			else if (numberKal == 3)	{
				text3.text = kalimat;				
				text3.setTextFormat(formatText);
			}
			
			else if (numberKal == 4)	{
				text4.text = kalimat;				
				text4.setTextFormat(formatText);
			}
		}
		
		public function stateGame(e:TimerEvent) {
			
		//  controller
			
		//  =====================================================
		//  FLOW
		//  =====================================================
			
			waktu += 1;			
			
			
			if (waktu == 15) {				
				text1.x = bubble2.x + 90;
				text1.y = bubble2.y + 80;
				setFormatText(1,"Ngapain ya??", 40, "First Grader", "0x947000");
			}
				
			changeStateIn();
			changeStateOut();	
			
		}
			
	//  =====================================================
	//  Main Menu FUNCTION
	//  =====================================================
		
		public function Menu1(e:Event) {
			state = 1;
			changeFinished = false;
		}
		
		public function Menu2(e:Event) {
			state = 2;
			changeFinished = false;
		}
		
		public function Menu3(e:Event) {
			state = 3;
			changeFinished = false;
		}
		
		public function mouseOver1(e:Event) {
			if (state == 0) {
				ayoBelajar.alpha = 1;
			}
		}
		
		public function mouseOut1(e:Event) {
			if (state == 0) {
				ayoBelajar.alpha = 0.7;
			}
		}
		
		public function mouseOver2(e:Event) {
			if (state == 0) {
				mariBermain.alpha = 1;
			}
		}
		
		public function mouseOut2(e:Event) {
			if (state == 0) {
				mariBermain.alpha = 0.7;
			}
		}
		
		public function mouseOver3(e:Event) {
			if (state == 0) { 
				latihanYuk.alpha = 1;
			}
		}
		
		public function mouseOut3(e:Event) {
			if (state == 0) {
				latihanYuk.alpha = 0.7;
			}
		}
		
		
	//  =====================================================
	//  Menu Belajar FUNCTION
	//  =====================================================
		
		public function Menu11(e:Event) {
			state = 11;
			changeFinished = false;
		}
		
		public function Menu12(e:Event) {
			state = 12;
			changeFinished = false;
		}
		
		public function Menu13(e:Event) {
			state = 13;
			changeFinished = false;
		}
		
		public function mouseOver11(e:Event) {
			if (state == 1) {
				bangunDatar.alpha = 1;				
				text2.x = bubble1.x + 90;
				text2.y = bubble1.y + 100;
				setFormatText(2,"Disini kita akan belajar \n bangun datar", 40, "First Grader", "0x947000");
			}
		}
		
		public function mouseOut11(e:Event) {
			if (state == 1) {
				bangunDatar.alpha = 0.7;
			}
		}
		
		public function mouseOver12(e:Event) {
			if (state == 1) {
				bilangan.alpha = 1;
				text2.x = bubble1.x + 80;
				text2.y = bubble1.y + 100;
				setFormatText(2,"Ayo, kita mengenal tentang \n bilangan", 40, "First Grader", "0x947000");
			}
		}
		
		public function mouseOut12(e:Event) {
			if (state == 1) {
				bilangan.alpha = 0.7;
			}
		}
		
		public function mouseOver13(e:Event) {
			if (state == 1) { 
				operasi.alpha = 1;
				text2.x = bubble1.x + 20;
				text2.y = bubble1.y + 100;
				setFormatText(2,"Kamu tahu cara operasi bilangan? \n ayo belajar disini", 40, "First Grader", "0x947000");
			}
		}
		
		public function mouseOut13(e:Event) {
			if (state == 1) {
				operasi.alpha = 0.7;
			}
		}
		
	//  =====================================================
	//  FLOW FUNCTION
	//  =====================================================
	
		public function changeStateOut() {
			if (state == 1) {
				if (ayoBelajar.alpha >= 0) {
					ayoBelajar.alpha -= 0.10;				
				}
				else ayoBelajar.visible = false;
				
				if (mariBermain.alpha >= 0) {
					mariBermain.alpha -= 0.10;				
				}
				else mariBermain.visible = false;
				
				if (latihanYuk.alpha >= 0) {
					latihanYuk.alpha -= 0.10;				
				}
				else {
					latihanYuk.visible = false;				
					bubble2.visible = false;
					text1.visible = false;
				}
			}
		}
		
		public function changeStateIn() {
			if (state == 0) {
				
				ayoBelajar.visible = true;
				mariBermain.visible = true;
				latihanYuk.visible = true;
				
				if (ayoBelajar.alpha <= 0.7) {
					ayoBelajar.alpha += 0.10;				
				}
				if (mariBermain.alpha <= 0.7) {
					mariBermain.alpha += 0.10;				
				}
				if (latihanYuk.alpha <= 0.7) {
					latihanYuk.alpha += 0.10;				
				}	
				else bubble2.visible = true;
			}
			
			else if (state == 1) {
				
				bangunDatar.visible = true;
				operasi.visible = true;
				bilangan.visible = true;
				
				if (bangunDatar.alpha <= 0.7){
					bangunDatar.alpha += 0.10;				
				}
				if (operasi.alpha <= 0.7) {
					operasi.alpha += 0.10;				
				}
				if (bilangan.alpha <= 0.7) {
					bilangan.alpha += 0.10;		
				}			
				else {
					if (!changeFinished) {
						bubble1.visible = true;
						text2.visible = true;	
						text2.x = bubble1.x + 90;
						text2.y = bubble1.y + 120;
						setFormatText(2, "Mau materi yang mana??", 40, "First Grader", "0x947000");
						soundchannel.stop();
						soundchannel = sound5.play(); // ntar diganti soundnya
						changeFinished = true;
					}
				}
			}
		}
	}
}
